import {
  Notify
} from 'vant';

export function isPhone (s) {
  return /^1[0-9]{10}$/.test(s)
}

export function isPass (password) {
  var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
  return reg.test(password)
}

export function isPay (password) {
  var reg = /^\d{6}$/
  return reg.test(password)
}

// 倒计时计算
export function formattime(time) {
  var leave1 = time % (24 * 3600 * 1000) //计算小时    
  var hour = Math.floor(leave1 / (3600 * 1000))

  var leave2 = leave1 % (3600 * 1000) //计算分钟    
  var minute = Math.floor(leave2 / (60 * 1000))

  var leave3 = leave2 % (60 * 1000) //计算秒    
  var second = Math.round(leave3 / 1000) == '60' ? '00' : Math.round(leave3 / 1000)
  return [hour, minute, second].map(formatNumber).join(':')
}

export function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 是否是ios
export function isIos() {
  var u = navigator.userAgent;
  if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
    return false
  } else if (u.indexOf('iPhone') > -1) { //苹果手机
    return true
  } else if (u.indexOf('iPad') > -1) { //iPad
    return false
  } else if (u.indexOf('Windows Phone') > -1) { //winphone手机
    return false
  } else {
    return false
  }
}

export function isAndroid() {
  var u = navigator.userAgent;
  if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
    return true
  } else {
    return false
  }
}

// js转换成时间格式
export function formatDate(dates) {
  var now = new Date(dates * 1000)
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var date = now.getDate();
  var hour = now.getHours();
  var minute = now.getMinutes();
  var second = now.getSeconds();
  return year + "-" + formatNumber(month) + "-" + formatNumber(date) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second);
}

// 复制
export function copyToClip(content, message) {
  var aux = document.createElement("input");
  aux.setAttribute("value", content);
  document.body.appendChild(aux);
  aux.select();
  document.execCommand("copy");
  document.body.removeChild(aux);
  if (message == null) {
    Notify({
      type: 'success',
      message: '复制成功'
    });
  } else {
    Notify({
      type: 'warning',
      message: '失败！'
    });
  }
}

// 加密
export function addPass(data) {
  let Base64 = require('js-base64').Base64
  //加密
  var str = Base64.encode(data);
  return str
}

export function reducePass(data) {
  let Base64 = require('js-base64').Base64
  var str = Base64.decode(data);
  return str
}